package davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.phone;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.logging.Logger;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.R;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.activity.BaseActivity;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.activity.PageActivity;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.adapter.MyAdapter_CommonAdapter;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.app.MyApplication;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.app.ViewHolder;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.callback.OkHttpStopCallback;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.phone.contants.Url;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.phone.entity.JianpanEntity;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.utils.LogUtil;
import davidandroidprojecttools.qq986945193.com.davidandroidprojecttools.utils.ToastUtils;
import okhttp3.Response;

/**
 * @author ：程序员小冰
 * @新浪微博 ：http://weibo.com/mcxiaobing
 * @GitHub: https://github.com/QQ986945193
 * @CSDN博客: http://blog.csdn.net/qq_21376985
 * @交流Qq ：986945193
 */

/**
 *
 */
public class CallActivity extends BaseActivity {


    private ImageView iv_hangup;

    private Chronometer c_timer;


    private TextView tv_number;
    private TextView tv_addr;
    private TextView tv_call_state;


    String id = "";

    private static CallActivity sInstance;


    public static boolean isInstanciated() {
        return sInstance != null;
    }

    public static CallActivity instance() {
        if (sInstance != null) {
            return sInstance;
        }
        return new CallActivity();

    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_call);
        sInstance = this;
        iv_hangup = (ImageView) findViewById(R.id.iv_hangup);
        c_timer = (Chronometer) findViewById(R.id.c_timer);
        tv_number = (TextView) findViewById(R.id.tv_number);
        tv_addr = (TextView) findViewById(R.id.tv_addr);
        tv_call_state = (TextView) findViewById(R.id.tv_call_state);
        tv_call_state.setText("正在拨号");
        tv_call_state.setVisibility(View.VISIBLE);


        c_timer.setVisibility(View.GONE);
        if (getIntent() != null) {
            id = getIntent().getStringExtra("id");
            String address = getIntent().getStringExtra("callPhoneAddress");
            String callPhoneNum = getIntent().getStringExtra("callPhoneNum");
            LogUtil.e("id = " + id);
            LogUtil.e("address = " + address);
            LogUtil.e("callPhoneNum = " + callPhoneNum);

            if (callPhoneNum.length() < 11 || callPhoneNum.length() > 11) {
                tv_number.setText(callPhoneNum);
                tv_addr.setText("未知地区");
            } else {
                StringBuffer sb = new StringBuffer(callPhoneNum);
                sb.insert(3, " ");
                sb.insert(8, " ");
                String s = sb.toString();

                tv_number.setText(s);
                tv_addr.setText(address);
            }


        }

        if ("未知地区".equals(tv_addr.getText().toString()) || TextUtils.isEmpty(tv_addr.getText().toString())) {

        } else {
//            new Handler().postDelayed(new Runnable() {
//                public void run() {
//                    //execute the task
//                    jieTong();
//                }
//            }, 5000); //5秒
        }


    }

    public void jieTong(final String msgId) {
        LogUtil.e("msgId = " + msgId);


        Handler mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                LogUtil.e("msgId = " + msgId.equals(id));
                //已在主线程中，可以更新UI
                tv_call_state.setVisibility(View.GONE);
                c_timer.setVisibility(View.VISIBLE);
                c_timer.setBase(SystemClock.elapsedRealtime());
                c_timer.start();

            }
        });


    }


    @Override
    protected void initData() {

    }

    @Override
    protected void setOnclickListener() {
        iv_hangup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(id)) {
                    if ("未知地区".equals(tv_addr.getText().toString()) || TextUtils.isEmpty(tv_addr.getText().toString())) {

                        finish();

                    } else {
                        Map<String, String> params = new HashMap<>();

                        params.put("id", id);
                        params.put("userId", MyApplication.getApp().getSN());

                        okHttpUtils.post(Url.PHONE_BASE_URL + "/call-phone-info/update", params, null, new OkHttpStopCallback<String>() {

                            @Override
                            public void onSuccess(Response response, String s) {
                                LogUtil.e("result = " + s);
                                finish();
                            }
                        });
                    }
                }


            }
        });
    }


}
